package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_CREATE, desc = "创建联盟")
public class CreateUnionCmd extends AbstractCommand {

	private String unionName;

	private int unionFlag;

	private boolean isAllowRandomJoin;

	private String outNotice;

	private String qqCode;

	private String wechatCode;

	private String innerNotice;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionCreateReqMsg reqMsg = UnionProto.UnionCreateReqMsg.parseFrom(packet.getBytes());
		unionName = reqMsg.getUnionName();
		unionFlag = reqMsg.getUnionFlag();
		outNotice = reqMsg.getOutNotice();
		qqCode = reqMsg.getQqCode();
		wechatCode = reqMsg.getWechatCode();
		isAllowRandomJoin = reqMsg.getIsAllowRandomJoin();
		innerNotice = reqMsg.getInnerNotice();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(player.isRequestToFast()){
			ret  =  GameErrorCode.E_REQUEST_TOO_FAST;
			return;
		}
		//编辑限制
		GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_UNION_CREATE);
		if(null != gameEditLimit){
			long nowTime = System.currentTimeMillis();
			if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
				String replaceStr1 = "没有最强的商会，只有最团结的商会！记得每日建设拿奖励！";
				String replaceStr2 = "欢迎加入我们，共同建设商会！";
				if(!StringUtils.isNullOrEmpty(gameEditLimit.getReplaceContent())){
					String[] replaceArr = gameEditLimit.getReplaceContent().split("\\|");
					if(replaceArr.length > 0){
						replaceStr1 = replaceArr[0];
					}
					if(replaceArr.length > 1){
						replaceStr2 = replaceArr[1];
					}
				}
				outNotice = replaceStr2;
				innerNotice = replaceStr1;
			}
		}
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}

		if (!StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {//判断有没有联盟
			ret = GameErrorCode.E_UNION_HAS_JOIN_UNION;
			return;
		}

		if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.UNION_CREATE_NEED_VIP_LEVEL){
			ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
			return;
		}

		if(!ConfigMgr.hasUnionFlag(unionFlag)){
			ret = GameErrorCode.E_UNION_FLAG_NO_FOUND;
			return;
		}

		AttributeModule attributeModule = player.getModule(AttributeModule.class);
		int freeCreateUnionTimes = (int) attributeModule.getAttribute(ePlayerAttrType.FreeCreateUnionTimes);
		if (player.getUserInfo().getVipExp() <= 0 || freeCreateUnionTimes >= GameConfig.UNION_FREE_CREATE_TIMES) {//没有充值的或者大于免费创建次数就需要消耗元宝
			Property consume = new Property();
			consume.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(GameConfig.UNION_CREATE_INGOTS_COST));
			if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
				ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
				return;
			}

		} else {

		}

		int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
		if(eChannelType.isMainLandChannel(channelId) && ChannelConfig.CN_FORBIDDEN_WORD_CHECK == 1) {//大陆渠道，实时检测内容是否屏蔽
			ret = UnionConfigMgr.checkUnionName(unionName, "createunion", player);
			if(0 != ret) {
				return;
			}
			ret = UnionConfigMgr.checkOutNotice(outNotice, "createunion", player);
			if(0 != ret) {
				return;
			}
		}

		UnionProto.CrossUnionCreateReqMsg.Builder crossReq = UnionProto.CrossUnionCreateReqMsg.newBuilder();
		crossReq.setQqCode("");
		crossReq.setWechatCode("");
		crossReq.setInnerNotice(innerNotice);
		crossReq.setOutNotice(outNotice);
		crossReq.setIsAllowRandomJoin(isAllowRandomJoin);
		crossReq.setUnionFlag(unionFlag);
		crossReq.setUnionName(unionName);
		crossReq.setPlayerBaseData(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
		player.sendPacket(Protocol.C_UNION_CREATE, crossReq);

	}



	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionCreateRespMsg.Builder respMsg = UnionProto.UnionCreateRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_UNION_CREATE, respMsg);
		}
	}
}
